package com.yikaiye.android.yikaiye.ui.activity.login.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.yikaiye.android.yikaiye.base.BaseViewModel;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.di.retrofit.BaseModel;
import com.yikaiye.android.yikaiye.di.retrofit.BaseNetCallback;
import com.yikaiye.android.yikaiye.model.VerifyCodeModel;

import java.util.HashMap;
import java.util.Map;

public class SendCodeViewModel extends BaseViewModel {
    /**
     * 验证码获取成功
     */
    public MutableLiveData<VerifyCodeModel> codeSuccess = new MutableLiveData<>();
    /**
     * 验证码获取失败
     */
    public MutableLiveData<String> codeError = new MutableLiveData<>();

    /**
     * 获取验证码
     */
    public void sendCode(int action, String number) {
        Map<String, Object> map = new HashMap<>();
        map.put("number", number);
        map.put("action", action);
        addSubscribe(mRetrofitHelper.sendCode(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<VerifyCodeModel>() {

            @Override
            public void onSuccess(BaseModel<VerifyCodeModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        codeSuccess.setValue(baseModel.getData());
                    }
                } else {
                    codeError.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                codeError.setValue(message);
            }
        }));
    }
}
